package com.dog.basics.service.mtkj.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.basics.bean.dto.api.mtkj.ApiBankAccountAddDto;
import com.dog.basics.bean.dto.api.mtkj.ApiBankAccountEditDto;
import com.dog.basics.bean.model.mtkj.MtBankAccount;
import com.dog.basics.bean.model.mtkj.MtGoodsEvaluate;
import com.dog.basics.bean.model.mtkj.MtShopping;
import com.dog.basics.bean.vo.api.mtkj.ApiBankAccountVo;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.dao.mtkj.MtBankAccountMapper;
import com.dog.basics.dao.mtkj.MtShoppingMapper;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.service.common.WeChatMiniService;
import com.dog.basics.service.mtkj.IMtBankAccountService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 绑定银行账户 服务实现类
 * </p>
 *
 * @author wph
 * @since 2020-11-09
 */
@Service
@AllArgsConstructor
public class MtBankAccountServiceImpl extends ServiceImpl<MtBankAccountMapper, MtBankAccount> implements IMtBankAccountService {
    private final MtShoppingMapper mtShoppingMapper;
    private final WeChatMiniService weChatMiniService;
    /**
     * @Description: 分页银行账户列表
     * @Param: status
     * @return: IPage<PlatePageVo>
     * @Author: wph
     * @Date:
     */
    @Override
    public IPage<ApiBankAccountVo> getPageList(PageParam page) {
        //获取当前用户的店铺ID
        List<MtShopping> shopList = mtShoppingMapper.selectList(Wrappers.<MtShopping>lambdaQuery().eq(MtShopping::getStatus, 1).eq(MtShopping::getUserId, SecurityUtils.getUser().getUserId()).orderByDesc(MtShopping::getCreateTime));
        Validator.validateTrue(CollUtil.isNotEmpty(shopList), "该用户还不是商家");
        IPage iPage = this.page(page, Wrappers.<MtBankAccount>lambdaQuery()
                .eq(MtBankAccount::getUserId, SecurityUtils.getUser().getUserId())
                .eq(MtBankAccount::getShopId, shopList.get(0).getId())
                .orderByDesc(MtBankAccount::getCreateTime));
        List<MtBankAccount> records = iPage.getRecords();
        iPage.setRecords(records.stream().map(e -> {
            ApiBankAccountVo UserAddrVo = new ApiBankAccountVo();
            BeanUtil.copyProperties(e, UserAddrVo);
            return UserAddrVo;
        }).collect(Collectors.toList()));
        return iPage;

    }

    /**
     * @Description: 增加银行账户
     * @Param: ApiBankAccountAddDto
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/05/29  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveObj(ApiBankAccountAddDto obj) {
        //获取当前用户的店铺ID
        List<MtShopping> shopList = mtShoppingMapper.selectList(Wrappers.<MtShopping>lambdaQuery().eq(MtShopping::getStatus, 1).eq(MtShopping::getUserId, SecurityUtils.getUser().getUserId()).orderByDesc(MtShopping::getCreateTime));
        Validator.validateTrue(CollUtil.isNotEmpty(shopList), "该用户还不是商家");
        MtBankAccount mtBankAccount = new MtBankAccount();
        BeanUtil.copyProperties(obj, mtBankAccount);
        mtBankAccount.setCreateTime(DateUtil.currentSeconds());
        mtBankAccount.setShopId(shopList.get(0).getId());
        mtBankAccount.setUserId(SecurityUtils.getUser().getUserId());
        mtBankAccount.setCreateBy(SecurityUtils.getUser().getUserId());
        Validator.validateTrue(this.save(mtBankAccount), "增加数据失败!");
        return Boolean.TRUE;
    }

    /**
     * @Description: 修改银行账户
     * @Param: ApiBankAccountEditDto
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/05/29  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean editObj(ApiBankAccountEditDto obj) {
        //获取当前用户的店铺ID
        List<MtShopping> shopList = mtShoppingMapper.selectList(Wrappers.<MtShopping>lambdaQuery().eq(MtShopping::getStatus, 1).eq(MtShopping::getUserId, SecurityUtils.getUser().getUserId()).orderByDesc(MtShopping::getCreateTime));
        Validator.validateTrue(CollUtil.isNotEmpty(shopList), "该用户还不是商家");
        MtBankAccount mtBankAccount = this.getById(obj.getId());
        Validator.validateNotNull(mtBankAccount, "无此数据");
        BeanUtil.copyProperties(obj, mtBankAccount);
        mtBankAccount.setUpdateTime(DateUtil.currentSeconds());
        mtBankAccount.setUpdateBy(SecurityUtils.getUser().getUserId());
        Validator.validateTrue(this.updateById(mtBankAccount), "修改数据失败!");
        return Boolean.TRUE;
    }

    /**
     * @Description: 删除银行账户
     * @Param: ids
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/05/29  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteObj(List<Long> ids) {
        //获取当前用户的店铺ID
        List<MtShopping> shopList = mtShoppingMapper.selectList(Wrappers.<MtShopping>lambdaQuery().eq(MtShopping::getStatus, 1).eq(MtShopping::getUserId, SecurityUtils.getUser().getUserId()).orderByDesc(MtShopping::getCreateTime));
        Validator.validateTrue(CollUtil.isNotEmpty(shopList), "该用户还不是商家");
        // 判断是否为空
        Validator.validateTrue(CollUtil.isNotEmpty(ids), "主键标识为空");
        Validator.validateTrue(this.removeByIds(ids), "删除数据失败!");
        return Boolean.TRUE;
    }

    @Override
    public Boolean cs(String openId, String keyword1, String keyword2) {
        try {
            weChatMiniService.pushMsg(openId,keyword1,keyword2);
        } catch (Exception e) {

        }
        return Boolean.TRUE;
    }
}
